Watch Teju sort arrays using Bubble, Selection, and Insertion Sort
Hi again! ๐ Today weโre learning three classic sorting algorithms:
Bubble Sort โ Repeatedly swap adjacent elements
Selection Sort โ Find minimum and place it in position
Insertion Sort โ Build sorted portion one by one
Largest elements "bubble up" to the end in each pass!
64 > 34 โ Swap! Largest numbers rise to the top
Time: O(nยฒ) | Best: O(n) when already sorted | Space: O(1)
In each pass, find the smallest element and swap it into place!
Scanning for minimum โ 25 is smallest โ swap with first unsorted
Time: Always O(nยฒ) | Space: O(1) | Fewer swaps than Bubble!
Build a sorted portion on the left โ insert new card in correct position!
Take 34 โ shift larger elements โ insert in correct spot
Best Case: O(n) nearly sorted | Worst: O(nยฒ) | Great for small or nearly sorted data!
| Algorithm | Time (Worst) | Time (Best) | Space | Stable? | Best For |
|---|---|---|---|---|---|
| Bubble Sort | O(nยฒ) | O(n) | O(1) | Yes | Educational |
| Selection Sort | O(nยฒ) | O(nยฒ) | O(1) | No | Fewer swaps needed |
| Insertion Sort | O(nยฒ) | O(n) | O(1) | Yes | Small or nearly sorted arrays |
Insertion Sort is usually the best among these three for real-world small/nearly-sorted data!